
package com.kugou.game.sdk.ui.widget;

import java.util.ArrayList;

import android.app.Dialog;
import android.content.Context;
import android.graphics.Bitmap;
import android.text.ClipboardManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ImageView.ScaleType;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.kugou.framework.v4.PagerAdapter;
import com.kugou.framework.v4.ViewPager;
import com.kugou.framework.v4.ViewPager.OnPageChangeListener;
import com.kugou.game.sdk.core.FolderManager;
import com.kugou.game.sdk.core.VipManager.GSInfo;
import com.kugou.game.sdk.utils.AppUtil;
import com.kugou.game.sdk.utils.AsyncImageLoader;
import com.kugou.game.sdk.utils.AsyncImageLoader.ImageCallback;
import com.kugou.game.sdk.utils.Rx;
import com.kugou.game.sdk.utils.ToastUtil;

/**
 * 描述:竖屏VIP弹窗
 *
 * @author jerryliu
 * @since 2016年1月22日 下午4:01:24
 */
public class VipDialogPortrait extends Dialog implements OnPageChangeListener {
    private Context context;

    ViewPager gsPager;

    LinearLayout pointLayout;

    TextView gsTitle, gsQQ;

    Button gsCopyBtn;

    ImageView gsClose;

    public VipDialogPortrait(Context context) {
        super(context, Rx.style.kg_KYTheme_Widget_Dialog);
        this.context = context;
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setCanceledOnTouchOutside(true);
        setCancelable(true);
        initViews(context);
    }

    private void initViews(final Context context) {
        View view = LayoutInflater.from(context).inflate(Rx.layout.kg_dialog_vip_layout_portrait,
                null);
        gsPager = (ViewPager) view.findViewById(Rx.id.gs_vip_dialog_pager);
        pointLayout = (LinearLayout) view.findViewById(Rx.id.gs_vip_dialog_point_layout);
        gsTitle = (TextView) view.findViewById(Rx.id.gs_tv_title);
        gsQQ = (TextView) view.findViewById(Rx.id.gs_tv_qq);
        gsCopyBtn = (Button) view.findViewById(Rx.id.gs_btn_copy);
        gsClose = (ImageView) view.findViewById(Rx.id.gs_close);
        gsClose.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (isShowing()) {
                    dismiss();
                }
            }
        });
        gsPager.setOnPageChangeListener(this);

        setContentView(view);
        getWindow().setLayout(AppUtil.dip2px(context, 240), AppUtil.dip2px(context, 334));
    }

    private ArrayList<GSInfo> gsData;

    public void setGsListData(ArrayList<GSInfo> data) {
        if (data != null && data.size() > 0) {
            this.gsData = data;

            GSAdapter adpater = new GSAdapter(context, data);
            gsPager.setAdapter(adpater);
            int size = data.size();
            for (int i = 0; i < size; i++) {
                ImageView dot = new ImageView(context);
                dot.setPadding(8, 8, 8, 8);
                pointLayout.addView(dot, ViewGroup.LayoutParams.WRAP_CONTENT,
                        ViewGroup.LayoutParams.WRAP_CONTENT);
            }
            showPage(0);
        }
    }

    /** 设置页面指示圆点的显示 */
    private void setDotSelect(int index) {
        for (int i = 0; i < pointLayout.getChildCount(); i++) {
            View view = pointLayout.getChildAt(i);
            if (view != null && view instanceof ImageView) {
                ((ImageView) view).setImageResource(Rx.drawable.kg_white_point);
            }
        }

        if (pointLayout.getChildAt(index) != null
                && pointLayout.getChildAt(index) instanceof ImageView) {

            ((ImageView) (pointLayout.getChildAt(index)))
                    .setImageResource(Rx.drawable.kg_blue_point);
        }
    }

    class GSAdapter extends PagerAdapter implements ImageCallback {
        Context contex;

        ArrayList<GSInfo> data;

        AsyncImageLoader imageLoader;

        public GSAdapter(Context context, ArrayList<GSInfo> data) {
            this.contex = context;
            this.data = data;
            imageLoader = new AsyncImageLoader();
        }

        @Override
        public int getCount() {
            return data == null ? 0 : data.size();
        }

        @Override
        public boolean isViewFromObject(View view, Object obj) {
            return view == obj;
        }

        ViewGroup parent;

        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            ImageView gsImage = new ImageView(context);
            gsImage.setScaleType(ScaleType.FIT_XY);
            parent = container;
            container.addView(gsImage);

            final GSInfo info = data.get(position);
            if (info != null) {
                // 显示图片
                String iconUrl = info.getImgUrl();
                String imgKey = iconUrl + "-" + position;
                String savePath = FolderManager.IMAGE_CACHE_FOLDER
                        + AppUtil.getFileNameFromUrl(iconUrl);
                gsImage.setTag(imgKey);
                Bitmap bitmap = imageLoader.loadImage(context, imgKey, iconUrl, savePath, this);
                if (bitmap == null) {
                    gsImage.setBackgroundResource(Rx.drawable.kg_gs_default_img);
                } else {
                    gsImage.setImageBitmap(bitmap);
                }
                // 复制按钮
                gsCopyBtn.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (context == null) {
                            return;
                        }
                        ClipboardManager cmb = (ClipboardManager) context
                                .getSystemService(Context.CLIPBOARD_SERVICE);
                        GSInfo gsinfo = (GSInfo) gsCopyBtn.getTag();
                        if (gsinfo != null) {
                            cmb.setText(gsinfo.getQq().trim());
                            ToastUtil.showMsg("复制成功");
                        }
                    }
                });
            }
            return gsImage;
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            container.removeView((View) object);
        }

        @Override
        public void imageLoaded(Bitmap bitmap, String key) {
            if (parent != null) {
                View view = parent.findViewWithTag(key);
                if (view != null && view instanceof ImageView && bitmap != null) {
                    ((ImageView) view).setImageBitmap(bitmap);
                }
            }
        }
    }

    @Override
    public void onPageScrollStateChanged(int arg0) {

    }

    @Override
    public void onPageScrolled(int arg0, float arg1, int arg2) {

    }

    @Override
    public void onPageSelected(int position) {
        showPage(position);
    }

    private void showPage(int position) {
        setDotSelect(position);
        GSInfo info = gsData.get(position);
        if (info != null) {
            gsTitle.setText(info.getName());
            gsQQ.setText(info.getQq());
            gsCopyBtn.setTag(info);
        }

    }
}
